﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.ChucVuCanBoLopService;
namespace eTMS.Controllers{
public class ChucVuCanBoLopControllerBase : CommonController{
	public ChucVuCanBoLopControllerBase()
		: base()
	{	
		this.dao = new ChucVuCanBoLopServiceClient();
	}
	~ChucVuCanBoLopControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private ChucVuCanBoLopEntity oChucVuCanBoLop;	
	public ChucVuCanBoLopEntity ChucVuCanBoLopObject{
		get{
			return oChucVuCanBoLop;
		}
		set{
			oChucVuCanBoLop=value;
		}
	}
	protected ChucVuCanBoLopServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSChucVuCanBoLop(false);			
			return _Table;
		}
	}
	public void LoadAllDSChucVuCanBoLop(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oChucVuCanBoLop==null)return false;
		bool result=true;
		try{
			oChucVuCanBoLop = dao.SaveRet(oChucVuCanBoLop);
		}
		catch{
			result=false;
		}
		if(result){
			_ListChucVuCanBoLop.Add(oChucVuCanBoLop);
		}
		return result;
	}
	public bool Update(){
		if(oChucVuCanBoLop==null)return false;
		return dao.Update(oChucVuCanBoLop);
	}
	public bool Delete(){
		if(oChucVuCanBoLop==null)return false;
		bool retvalue = dao.Delete(oChucVuCanBoLop);
		if(retvalue){			
			_ListChucVuCanBoLop.Remove(oChucVuCanBoLop);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 ChucVuCanBoLop_ID){
		bool retvalue = dao.DeleteByID(ChucVuCanBoLop_ID);		
		if(retvalue){
			ChucVuCanBoLopEntity oDeleted=FindChucVuCanBoLop(ChucVuCanBoLop_ID);
			if(oDeleted!=null){
				_ListChucVuCanBoLop.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oChucVuCanBoLop = new ChucVuCanBoLopEntity();
	}
    //Copy data from data row in grid to object
    public ChucVuCanBoLopEntity MapDataRowToObject(DataRow row){
		ChucVuCanBoLopEntity o = new ChucVuCanBoLopEntity();
		 o.TenChucVu=Convert.ToString(row["TenChucVu"]);
		 if(!row.IsNull("ChucVuCanBoLop_ID"))
		    o.ChucVuCanBoLop_ID=(Int32)(row["ChucVuCanBoLop_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(ChucVuCanBoLopEntity o,DataRow row){
		row["TenChucVu"]=o.TenChucVu;
		row["ChucVuCanBoLop_ID"]=o.ChucVuCanBoLop_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.ChucVuCanBoLopObject,row);
    }
	public void LoadObject(Int32 ChucVuCanBoLop_ID){
		ChucVuCanBoLopEntity oLoad=FindChucVuCanBoLop(ChucVuCanBoLop_ID);
		if(oLoad==null){
			oChucVuCanBoLop=this.LoadChucVuCanBoLop(ChucVuCanBoLop_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oChucVuCanBoLop.IsNullChucVuCanBoLop_ID;
			if(!bNotIsNull){
				this.ListChucVuCanBoLop.Add(oChucVuCanBoLop);
			}
		}
		else{
			oChucVuCanBoLop=oLoad;
		}
	}
	#region CachedList ChucVuCanBoLop
	private static List<ChucVuCanBoLopEntity> _ListChucVuCanBoLop=new List<ChucVuCanBoLopEntity>();
    public List<ChucVuCanBoLopEntity>ListChucVuCanBoLop{
		get{
			return _ListChucVuCanBoLop;
		}
	}
	public static List<ChucVuCanBoLopEntity>ListChucVuCanBoLopStatic{
		get{
			return _ListChucVuCanBoLop;
		}
	}
	public ChucVuCanBoLopEntity FindChucVuCanBoLop(Int32 ChucVuCanBoLop_ID){
		ChucVuCanBoLopEntity oResult=this.ListChucVuCanBoLop.Find(delegate(ChucVuCanBoLopEntity oFind){
			bool result=true; 
			result&=oFind.ChucVuCanBoLop_ID.Equals(ChucVuCanBoLop_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllChucVuCanBoLop(bool clearCache){
		if(_ListChucVuCanBoLop.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		ChucVuCanBoLopServiceClient dao = new ChucVuCanBoLopServiceClient();
		_ListChucVuCanBoLop=new List<ChucVuCanBoLopEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public ChucVuCanBoLopEntity LoadChucVuCanBoLop(Int32 ChucVuCanBoLop_ID){			
		ChucVuCanBoLopEntity o=dao.SelectOne(ChucVuCanBoLop_ID);		
		return o;
	}
	#endregion
}
}
